package com.apps.common.model.po;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.Calendar;

@JsonInclude(Include.NON_NULL)
@ApiModel
@Data
public class ApiResult<T> implements Serializable {

	private static final long serialVersionUID = -8810954049370861490L;

	@ApiModelProperty(value = "状态码")
	public int status;
	@ApiModelProperty(value = "响应结果")
	public String msg;
	@ApiModelProperty(value = "系统当前时间")
	public long time;
	@ApiModelProperty(value = "返回数据")
	public T result;

	public ApiResult() {
		super();
	}

	public ApiResult(int status, String msg, T result) {
		super();
		this.status = status;
		this.msg = msg;
		this.result = result;
		this.time=Calendar.getInstance().getTimeInMillis()/1000;
	}

	public ApiResult(T result) {
		super();
		this.status = Ar.SUCCESS.ordinal();
		this.msg = Ar.SUCCESS.getMsg();
		
		this.time=Calendar.getInstance().getTimeInMillis()/1000;
		this.result = result;
	}

	public ApiResult<T> success(T result) {
		this.status = Ar.SUCCESS.ordinal();
		this.msg = Ar.SUCCESS.getMsg();
		this.time=Calendar.getInstance().getTimeInMillis()/1000;
		this.result = result;
		return this;
	}

	public ApiResult<T> success() {
		this.status = Ar.SUCCESS.ordinal();
		this.msg = Ar.SUCCESS.getMsg();
		this.time=Calendar.getInstance().getTimeInMillis()/1000;
		return this;
	}

	public ApiResult<T> succMsg(String msg) {
		this.status = Ar.SUCCESS.ordinal();
		if (null == this.msg) {
			this.msg = msg;
		} else {
			this.msg = msg;
		}
		this.time=Calendar.getInstance().getTimeInMillis()/1000;
		return this;
	}

	public ApiResult<T> dataError() {
		this.status = Ar.DATAERROR.ordinal();
		this.msg = Ar.DATAERROR.getMsg();
		return this;
	}

	public ApiResult<T> dataError(String msg) {
		this.status = Ar.DATAERROR.ordinal();
		if (null == this.msg) {
			this.msg = msg;
		} else {
			this.msg = msg;
		}
		return this;
	}

	public ApiResult<T> dataError(int error,String msg) {
		this.status = error;
		if (null == this.msg) {
			this.msg = msg;
		} else {
			this.msg = msg;
		}
		return this;
	}

	public ApiResult<T> failMsg(String msg) {
		this.status = Ar.FAIL.ordinal();
		if (null == this.msg) {
			this.msg = msg;
		} else {
			this.msg = msg;
		}
		this.time=Calendar.getInstance().getTimeInMillis()/1000;
		return this;
	}

	public ApiResult<T> fail(String msg) {
		this.status = Ar.FAIL.ordinal();
		this.msg = msg;
		this.time=Calendar.getInstance().getTimeInMillis()/1000;
		return this;
	}

	public ApiResult<T> fail() {
		this.status = Ar.FAIL.ordinal();
		this.msg = Ar.FAIL.getMsg();
		this.time=Calendar.getInstance().getTimeInMillis()/1000;
		return this;
	}

	public ApiResult<T> unauthorized() {
		this.status = Ar.NOLOGIN.ordinal();
		this.msg = Ar.NOLOGIN.getMsg();
		return this;
	}
	
	public ApiResult<T> invalid() {
		this.status = Ar.INVALID.ordinal();
		this.msg = Ar.INVALID.getMsg();
		return this;
	}

	public String toStr() {
		return "{\"status\":" + status + ",\"msg\":\"" + msg + "\"}";
	}

}
